//代表加载页面的时候去执行里面的方法
$(function () {
    $("#page").load("/back/page.html");
    findAll();
})

// function findAll() {
//     let data = {
//         pageNum: $.trim($("#pageNum").val()),
//         pageSize: $.trim($("#pageSize").val())
//     };
//     $.ajax({
//         url: '/back/user/findAll',
//         data: data,
//         type: 'get',
//         dataType: 'json',
//         success: function (data) {
//             data = data.list;
//             setData(data);
//         }
//     })
// }

function setData(data) {
    let html = '';//字符串拼接
    for (let i = 0; i < data.length; i++) {
        html += ' <tr>\n' +
            '        <td>' + data[i].id + '</td>\n' +
            '        <td>' + data[i].username + '</td>\n' +
            '        <td>' + (data[i].birthday == undefined ? '' : data[i].birthday) + '</td>\n' +
            '        <td>' + (data[i].introduce == undefined ? '' : data[i].introduce) + '</td>\n' +
            '        <td>' + (data[i].hobby == undefined || data[i].hobby == 'null' ? '' : data[i].hobby) + '</td>\n' +
            '        <td>' + (data[i].sex == undefined ? '' : data[i].sex) + '</td>\n' +
            '        <td>' + (data[i].phone == undefined ? '' : data[i].phone) + '</td>\n' +
            '        <td><a href="javascript:edit(' + data[i].id + ')">编辑</a> <a href="javascript:del(' + data[i].id + ')">删除</a> </td>\n' +
            '    </tr>';
    }
    $("#userFindAll").html(html);//对当前的tbody进行赋值
}

function edit(id) {
    localStorage.setItem("userId", id);
    sessionStorage.setItem("userId", id);
    $('#right').load('/back/userEdit.html');
}

function del(id) {
    $.ajax({
        url: '/back/user/delete',
        data: {id: id},
        type: 'get',
        dataType: 'json',
        success: function (data) {
            if (data.count == 1) {
                findAll();
                // $('#right').load('/back/userManage.html');
            } else {
                alert("删除失败");
            }
        }
    })
}

//根据条件查询
function findAll() {
    let data = {};
    data.pageNum = $.trim($("#pageNum").val());
    data.pageSize = $.trim($("#pageSize").val());
    data.username = $.trim($("#name").val());
    data.startBirthday = $("#startBirthday").val();
    data.endBirthday = $("#endBirthday").val();
    data.introduce = $.trim($("#introduce").val());
    data.hobby = JSON.stringify(aihao());
    data.sex = $("#sex").find("option:selected").text();
    data.phone = $.trim($("#phone").val());
    console.log(data);
    $.ajax({
        url: '/back/user/findAllBySome',
        data: data,
        type: 'get',
        dataType: 'json',
        success: function (data) {
            console.log(data)
            setData(data.list);
            $("#pageCount").html(data.number);
        }
    })
}

function aihao() {
    let hb = [];
    $("input:checkbox[name='hobby']").each(function () {
        console.log();
        if ($(this).prop("checked")) {
            hb.push($(this).val());
        }
    })

    return hb;
}